package wechat.rcv;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import wechat.rcv.cosuming.RequestObj;
import wechat.rcv.cosuming.WXRequestTree;
import wechat.rcv.util.Log4jService;
import wechat.rcv.util.RcvToken;
import weixin.mp.aes.AesException;
import weixin.mp.aes.SHA1;

@RestController
@RequestMapping(value="/serviceapp")
@ResponseBody
@Transactional
public class ServiceAppFrontController {
	final static Logger logger = Logger.getLogger(ServiceAppFrontController.class.getName());
	@Autowired
	WXRequestTree wxRequestTree;
	
	@Autowired
	private Log4jService log4jService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String ServiceAppUrlValidation(@RequestParam(value = "msg_signature", defaultValue = "") String msg_signature,
			@RequestParam(value = "timestamp", defaultValue = "") String timestamp,
			@RequestParam(value = "nonce", defaultValue = "") String nonce,
			@RequestParam(value = "echostr", defaultValue = "") String echostr) {
		log4jService.info(ServiceAppFrontController.class,"***GET /serviceapp Request received!***");
		logger.info("msg_signature = "+ msg_signature);
		logger.info("timestamp = "+ timestamp);
		logger.info("nonce = "+ nonce);
		logger.info("echostr = "+ echostr);
		String sEchoStr = echostr;
//		try {
////			RemoveJCELimit.removeCryptographyRestrictions();
//			sEchoStr = RcvToken.getServiceAppWxcpt().VerifyURL(msg_signature, timestamp, nonce, echostr);
//		} catch (AesException e) {
//			// TODO Auto-wechat.rcv.generated catch block
//			e.printStackTrace();
//		}
//        String[] str = { RcvToken.sServiceAppToken, timestamp, nonce };
//        Arrays.sort(str); // 字典序排序
//        String bigStr = str[0] + str[1] + str[2];
        
        // SHA1加密
//        String digest = SHA1.getDigestOfString(bigStr.getBytes()).toLowerCase();		
		return sEchoStr;
	}
	@RequestMapping(method = RequestMethod.POST, headers = {
			"Accept=application/xml;charset=UTF-8" }, produces = MediaType.APPLICATION_XML_VALUE)
	public String CallbackRequest(@RequestBody String input) {
		logger.info("************POST Request received!************");
		logger.info(input);
		logger.info("************POST Request end!************");

		try {
			RequestObj req = new RequestObj(input);
			return assertNotNull(wxRequestTree.getHandler(req)).acceptSync();
		}catch (RuntimeException e) {
			// TODO Auto-wechat.rcv.generated catch block
			e.printStackTrace();
			logger.error("getHandler return null");
		} 
		catch (Exception e) {
			// TODO Auto-wechat.rcv.generated catch block
			logger.error(e.getMessage());
		}
		return input;
	}
	private <T> T assertNotNull(T t){
		
		if(t==null){ 
			throw new RuntimeException();
		}
		return t;
	}
}